using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Camalon.ColorPicker
{
	/// <summary>
	/// Summary description for HexadecimalUpDown.
	/// </summary>
    [ToolboxItem(false)]
    public class ColorValueUpDown : System.Windows.Forms.NumericUpDown
	{
		protected override void OnLeave(EventArgs e)
		{
			base.OnLeave (e);
			OnValueChanged( new EventArgs());
		}


		private int _maxTextLength = 3;
		protected int MaxTextLength
		{
			set{ _maxTextLength=value;}
			get{ return _maxTextLength;}
		}

		private bool	_changeLocked;
		string			_oldText	= string.Empty;
		protected override void OnTextBoxTextChanged(object source, EventArgs e)
		{
			if( _changeLocked ) return;

		
			_changeLocked = true;
			if(this.Text.Length > _maxTextLength )
			{
				this.Text = _oldText;
				base.ValidateEditText();
				base.Select( _oldText.Length,0);
			}
			else if(this.Text.Length == 0 )
			{
				this.Text = _oldText;
				base.ValidateEditText();
				base.Select( 0,1);
			}
			else
			{
				base.ValidateEditText();
				base.Select( this.Text.Length,0);
			}

			
			_oldText = this.Text;
			_changeLocked = false;

		}

	}
}
